package burst;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TestDB {
	public static void main(String args[]) throws Exception {
		Class.forName("com.ibm.db2.jcc.DB2Driver");
		try {
			int total = 10;
			Thread[] thrds = new Thread[total];
			for (int i = 0; i < total; i++) {
				Connection conn = DriverManager.getConnection(
						"jdbc:db2://172.16.4.33:60000/scdb", "scread",
						"sc09");
				thrds[i] = new TestDBThread(conn);
			}
			for (int i = 0; i < total; i++) {
				thrds[i].start();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
	}
}

class TestDBThread extends Thread implements Runnable {
	Connection conn = null;

	public TestDBThread(Connection conn) {
		this.conn = conn;
	}

	public void run() {
		try {
			long t1 = System.currentTimeMillis();
			PreparedStatement pstmt = conn
					.prepareStatement(new StringBuffer(200).append("select * from (").append( 
"select ID,YYLY,DW_ID,QXBH,XL_ID,DYDJ,CBQXDJ,ZZQXDJ,QXSBMC,QXSBBH,QXLY,QXNR,QXYY,QXDDMS,YYCBFX,CBCLFA,FXRQ,YQWCRQ,XQRQ,SFQX,QXZT,FXR_ID,FXRMC,BZ_ID,BZMC,BJMC,FXDD,CLFS,BDZ_ID,SB_ID,HSB_ID,SFXYFY,JSYY,ZRYY,BZ,JLZT,SFGTQX,YSRQ ,DL_ID,DLMC, rownumber() over (order by id desc) as rn from "
).append("(SELECT T00.ID,T00.YYLY,T00.DW_ID,T00.QXBH,T00.XL_ID,T00.DYDJ,T00.CBQXDJ,T00.ZZQXDJ,T00.QXSBMC,T00.QXSBBH,T00.QXLY,T00.QXNR,T00.QXYY,T00.QXDDMS,T00.YYCBFX,T00.CBCLFA,T00.FXRQ,T00.YQWCRQ,T00.XQRQ,T00.SFQX,T00.QXZT,T00.FXR_ID,T00.FXRMC,T00.BZ_ID,T00.BZMC,T00.BJMC,T00.FXDD,T00.CLFS,T00.BDZ_ID,T00.SB_ID,T00.HSB_ID,T00.SFXYFY,T00.JSYY,T00.ZRYY,T00.BZ,T00.JLZT,T00.SFGTQX,E.YSRQ ,T00.DL_ID,T00.DLMC "
).append("FROM USERID.T_QX_JBXX T00 LEFT JOIN "
).append("(SELECT T01.QX_ID QX_ID,MAX(T01.YSRQ) AS YSRQ FROM USERID.T_QX_QXYS  AS T01 "
).append("GROUP BY T01.QX_ID) E ON E.QX_ID = T00.ID where xl_id is not null) "
//).append("LEFT JOIN USERID.T_SBMX_LNODE AS T02 ON XL_ID = T02.ID) " 
).append("as tmp "
).append(")as tmp_table where rn between 1 and 30").toString());
			ResultSet rs = pstmt.executeQuery();
			for (int i = 0; i < 30; i++) {
				rs.next();
				PreparedStatement pstmt2 = conn.prepareStatement("SELECT LNODEMC FROM USERID.T_SBMX_LNODE WHERE ID=?");
				//System.out.println(rs.getLong("XL_ID"));
				pstmt2.setLong(1, rs.getLong("XL_ID"));
				ResultSet rs2 = pstmt2.executeQuery();
				if (rs2.next()) {
					System.out.println("GOT NAME: "+rs2.getString(1));
				}
				pstmt2 = conn.prepareStatement("SELECT NAME FROM USERID.SYS_SPECIALORG WHERE ID=?");
				pstmt2.setLong(1, rs.getLong("DW_ID"));
				rs2 = pstmt2.executeQuery();
				if (rs2.next()) {
					System.out.println("GOT NAME: "+rs2.getString(1));
				}
				//System.out.println(Thread.currentThread().getName() + ":"
					//	+ rs.getString(1));
			}
			long t2 = System.currentTimeMillis();
			System.out.println(Thread.currentThread().getName() + " ********** :"
					+ (t2-t1));
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

	}

}
